package com.ujoku.controller.admin;

import com.labillusion.core.platform.cdn.upyum.UpYun;
import com.labillusion.core.platform.web.rest.RESTController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import com.ujoku.domain.Store;
import com.ujoku.service.StoreService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@RestController
public class StoreMngController extends RESTController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private UpYun upyun;

    @Value("#{configProperties['settings.store.upload']}")
    private String uploadPath;

    @Value("#{configProperties['settings.cdn.store.upload']}")
    private String cdnUploadPath;

    @RequestMapping(value="/uj-admin/store/list", method = RequestMethod.GET)
    public List<Store> storeList(){

        return storeService.selectList();
    }

    @RequestMapping(value = "/uj-admin/store/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Store getById(@PathVariable String id){
        return (Store)storeService.selectById(id);
    }

    @RequestMapping(value = "/uj-admin/store", method = RequestMethod.POST)
    @ResponseBody
    public Store add(@Valid @RequestBody Store store){

        storeService.insert(store);
        return store;
    }

    @RequestMapping(value = "/uj-admin/store", method = RequestMethod.PUT)
    @ResponseBody
    public Store update(@Valid @RequestBody Store store){

        storeService.update(store);
        return store;
    }

    @RequestMapping(value = "/uj-admin/store/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable int id, HttpServletRequest request) throws IOException {

        storeService.deleteById(id);

        String dir = request.getRealPath(uploadPath);
        String path = dir+"/"+ id + ".jpg";

        File  localFile = new File(path);
        upyun.deleteFile(cdnUploadPath + localFile.getName());

        return "successfully";
    }

    @RequestMapping(value="/uj-admin/store/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(  @RequestParam MultipartFile file,
                           @RequestParam String id, HttpServletRequest request) throws IOException {

        Store store =  (Store)storeService.selectById(id);
        //更新时间戳
        storeService.update(store);

        String dir = request.getRealPath(uploadPath);
        File folder = new File(dir);
        if(!folder.exists()){
            folder.mkdirs();
        }

        String originalPath = dir+"/"+ store.getId() + ".jpg";
        File originalFile = new File(originalPath);
        //将上传文件写入到指定文件出
        file.transferTo(originalFile);

        String path = dir+"/"+ store.getId() + ".jpg";

        File  localFile = new File(path);
        upyun.setContentMD5(UpYun.md5(localFile));
        upyun.writeFile(cdnUploadPath + localFile.getName(), localFile, true);


        return "Successfully";
    }

}
